package com.network.controller;

import com.google.gson.Gson;
import com.network.entity.Dept;
import com.network.entity.Plan;
import com.network.entity.User;
import com.network.qo.PageQo;
import com.network.service.PlanService;
import com.network.util.BaseData;
import com.network.util.CommonUtils;
import com.network.vo.PageResult;
import com.network.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
public class PlanController
{
    Logger logger= LoggerFactory.getLogger(PlanController.class);

    PageResult pageResult=null;
    Result result=null;

    @Autowired
    PlanService planService;
    @RequestMapping(value = "/addPlan",method = RequestMethod.POST,produces = "application/json;charset=utf-8;")
    public CompletableFuture<String> addPlan(Plan plan)
    {

        return planService.save(plan).thenApply(planTemp ->{
            Result result=null;
            if (CommonUtils.isNotNull(planTemp)) {
                result=new Result();
                result.setCode(200);
                result.setMsg("添加任务保存成功");
                result.setData(planTemp);
                BaseData.plans.put(planTemp.getId(),planTemp);
            }else
            {
                result.setCode(400);
                result.setMsg("添加任务失败");
            }
            return  new Gson().toJson(result);
        });

    }

    @RequestMapping(value = "/del/Plan",method = RequestMethod.GET,produces = "application/json;charset=utf-8;")
    public Result delPlanByPid(Long id)
    {
        result=new Result();
        try {
            Plan plan=planService.getPlanById(id);
            planService.delPlanById(id);
            result.setCode(200);
            result.setMsg("删除成功");
            Plan isok=BaseData.plans.remove(plan.getId());
            BaseData.test.remove(plan.getId());
            logger.info("*****************删除是否成功"+isok.getId());
            ;
        }catch (Exception e)
        {
            result.setCode(400);
            result.setMsg("删除失败");
        }finally {
            return result;
        }
    }

//    @RequestMapping(value = "/get/getPlanByPage",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
//    public PageResult getPlanByPage(PageQo pageQo)
//    {
//
//
//        Page<Plan> plans= planService.findPlanByPage(pageQo);
//
//        pageResult=new PageResult();
//        pageResult.setCode(0);
//        pageResult.setData(plans.getContent());
//        return pageResult;
//    }
}
